package com.aaa.phone.service.postionservice.impl;

import com.aaa.entity.AppBatteryOrder;
import com.aaa.entity.BatteryPosition;
import com.aaa.entity.Position;
import com.aaa.phone.dao.positiondao.PositionDao;
import com.aaa.phone.service.postionservice.PositionService;
import com.aaa.util.OrderNum;
import com.aaa.web.commons.Result;
import com.aaa.web.dao.iemployeedao.IPayDao;
import com.aaa.web.systemconst.ISystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * Demo class
 *  @author kevin
 *  @date 2018/12/17 20:35:33
 *
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionDao positionDao;

    @Autowired
    private IPayDao payDao;

    /***
     * 获取当前位置,并查询当前城市的电瓶点
     * @param position
     * @return
     */
    @Override
    public List<BatteryPosition> getPosition(Map<String, Position> position) {

        Position position1 = position.get("position");
        List<BatteryPosition> batteryPositions = positionDao.getAllPosition(position1);
        return batteryPositions;
    }

    /**
     * @Description: 根据所在地址获取当前服务区服务号码
     * @Param:
     * @return: 
     * @Author: Kevin Jia
     * @Date: 2018/12/20
     * @Time: 19:14
     */
    @Override
    public Map getServicePhone(Map<String,String> phone) {
        Map map = new HashMap();
        String phone1 = phone.get("phone");
        String type = phone.get("type");
        //查询改用户是否购买车辆
        Integer carIsExit = positionDao.serchCar(phone1);
        if (carIsExit == 0){
            map.put("code",ISystemConst.DEFEAT_CODE);
            map.put("msg","您暂不能使用该功能,如有问题请跟客服联系!");
            return map;
        }
        if (ISystemConst.MONTH_CODE.equals(type)){
            map.put("data", positionDao.getServicePhoneG());
            return map;
        }
        map.put("data", positionDao.getServicePhone(phone1));
        return map;
    }

    /**
     * @Description: 更换电瓶操作
     * @Param: addName
     * @return: void
     * @Author: Kevin Jia
     * @Date: 2018/12/26
     * @Time: 9:23
     */
    @Override
    public Result changeBatteryCount(String pid) {
        Result result = new Result();
            //扫码发来的请求,查询是否有可用电瓶
        Integer bQCounts = positionDao.getBatteryQuantitys(pid);
        //如果有返回成功信息
        if (bQCounts >0){
            //插入充电表
            positionDao.insertBattery(pid);
            //可用电瓶数量-1
            positionDao.changeBatteryCount(pid);
            result.setCode(ISystemConst.SUCCESS_CODE);
            result.setMsg("成功");
            return result;
        }

        //否则返回失败信息
        result.setCode(ISystemConst.DEFEAT_CODE);
        result.setMsg("暂无可用电瓶");
        return result;

    }

    /**
     * @Description: 
     * @Param:
     * @return: 
     * @Author: Kevin Jia
     * @Date: 2018/12/28
     * @Time: 18:55
     */
    @Override
    public Result getBatteryQuantity(String pid) {
        Result result = new Result();
        Integer batteryQuantity = positionDao.getBatteryQuantity(pid);
        if (batteryQuantity>0){
            result.setCode(ISystemConst.SUCCESS_CODE);
        }else{
            result.setCode(ISystemConst.DEFEAT_CODE);
            result.setMsg("当前站点可用电瓶数量不足,请等待!");
        }
        return result;
    }

    /**
     * @Description: 交易
     * @Param: telephone,money
     * @return: Result
     * @Author: Kevin Jia
     *
     * @Date: 2018/12/28
     * @Time: 19:35
     */
    @Override
    @Transactional
    public Result toDeal(String telephone, String money,String pid) {
            //添加交易记录,生成订单号
        String orderNumber = OrderNum.createOrderNumber();
            //扣除用户账户里的余额的钱
        positionDao.deductRMB(telephone,money);
            //插入消费记录(后台)
        AppBatteryOrder appBatteryOrder = new AppBatteryOrder();
        appBatteryOrder.setOrderid(orderNumber);
        appBatteryOrder.setOrdermoney(money);
        appBatteryOrder.setOrdername("共享电瓶订单");
        payDao.addAppOrder(appBatteryOrder);
        payDao.updateState();
        //插入消费记录(用户)
        positionDao.insertExpenseOrder(orderNumber,telephone,money);
            //站点数量-1
        positionDao.changeBatteryCount(pid);
            //插入充电表
        positionDao.insertBattery(pid);

            Result result = new Result();
            result.setCode(ISystemConst.SUCCESS_CODE);
            result.setMsg("交易成功");

        return result;
    }


}
